package com.eascs.p2p.admin.manager.impl;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.p2p.admin.manager.IDatadicManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.DatadicEntity;
import com.eascs.p2p.common.entity.DatadicItemEntity;
import com.eascs.p2p.common.vo.DatadicVO;

import jodd.util.StringUtil;

@Repository
public class DatadicManagerImpl extends BaseDao implements IDatadicManager {

	@Override
	public PageResult<Object[]> pageFindDatadic(DatadicVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		 sql.append("SELECT id,dd_code,dd_name,dd_desc from p2p_datadic where 1=1 and id<>16");
		
		
		//项目编号
		if (StringUtil.isNotBlank(vo.getCode())) {
			sql.append(" and dd_code like ?");
			sqlPageQuery.addParam("%" + vo.getCode() + "%");
		}
		// 名称
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and dd_name like ?");
			sqlPageQuery.addParam("%" + vo.getName() + "%");
		}
		
		sql.append(" and is_lock= 0 order by id desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

	@Override
	public PageResult<Object[]> queryDatadicItemByID(DatadicVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT id,item_code,item_name,item_desc,item_order from p2p_datadic_item where dd_no= ?");
		sqlPageQuery.addParam( vo.getId() );
		sql.append(" and is_lock= 0 and dd_no<>'16' order by id desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

	@Override
	public String addDatadic(DatadicEntity entity) {
		
		boolean flag = this.insert( entity);
		if (flag == true) {
			return  entity.getId();
		} else {
			return null;
		}

	}

	@Override
	public boolean updateDatadic(DatadicEntity entity) {
		
		 return this.updateById(entity);
	}

	@Override
	public boolean deleteDatadic(String id) {
		String sql="update p2p_datadic set is_lock=1 where id=?" ;
		return this.execute(sql, id);
	}

	@Override
	public String addDatadicItem(DatadicItemEntity entity) {
		boolean flag = this.insert( entity);
		if (flag == true) {
			return  entity.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateDatadicItem(DatadicItemEntity entity) {
		 return this.updateById(entity);
	}

	@Override
	public boolean deleteDatadicItem(String id) {
		String sql="update p2p_datadic_item set is_lock=1 where id=?" ;
		return this.execute(sql, id);
	}

	@Override
	public DatadicEntity findDatadicByID(String id) {
		
		return this.getById(DatadicEntity.class, id);
	}

	@Override
	public DatadicItemEntity findDatadicItemByID(String id) {
		return this.getById(DatadicItemEntity.class, id);
	}

	@Override
	public Integer isExistDatadic(String ddCode) {
		String sql="select count(1) from p2p_datadic where is_lock =0 and dd_code=? ";
		return this.findInteger(sql, ddCode);
	}

	@Override
	public Integer isExistDatadicItem(String ddNo, String itemCode) {
		String sql="select count(1) from p2p_datadic_item where is_lock =0 and dd_no =? and item_code=? ";
		return this.findInteger(sql, ddNo,itemCode);
	}


	

	

}
